﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Core.Utility.Attributes
{
    /// <summary>
    /// <see cref="DataColumn"/> 的特性
    /// </summary>

    [AttributeUsage(AttributeTargets.All, AllowMultiple = false)]
    public class DataColumnAttribute : Attribute
    {

        private string? caption;

        /// <summary>
        /// 通过附加属性定义 <see cref="DataColumn"/>
        /// </summary>
        public DataColumnAttribute()
        {

        }

        /// <summary>
        /// 将Model转换为DataColumn的附加属性
        /// </summary>
        /// <param name="columnName"><see cref="DataColumn.ColumnName"/></param>
        /// <param name="caption"><see cref="DataColumn.Caption"/></param>
        /// <param name="dataType"><see cref="DataColumn.DataType"/></param>
        /// <param name="order"><see cref="DataColumn.Ordinal"/></param>
        /// <param name="ignore">是否忽略</param>
        public DataColumnAttribute(
            string columnName = ""
            ,string caption = ""
            , Type? dataType = null
            , int order = 0
            , bool ignore = false)
        {
            this.ColumnName = columnName;
            this.Caption = caption;
            if(dataType == null)
            {
                this.DataType = typeof(object);
            }
            this.Order = order;
            this.Ignore = ignore;
        }

        /// <summary>
        /// 属性名称 主要用于Model转DataColumn时使用
        /// </summary>
        public string? PropertyName { get; set; }

        /// <summary>
        /// 忽略字段
        /// </summary>
        public bool Ignore { get; set; } = false;

        /// <summary>
        /// 列排序
        /// </summary>
        public int Order { get; set; } = 0;

        /// <summary>
        /// 列标题 <see cref="DataColumn.Caption"/>
        /// </summary>
        public string? Caption 
        {
            get => string.IsNullOrEmpty(caption) ? ColumnName : caption;
            set => caption = value; 
        }

        /// <summary>
        /// 列说明
        /// </summary>
        public string? Description { get; set; }

        /// <summary>
        /// 列名称 <see cref="DataColumn.ColumnName"/>
        /// </summary>
        public string? ColumnName { get; set; }

        /// <summary>
        /// 列数据类型 <see cref="DataColumn.DataType"/>
        /// </summary>
        public Type? DataType { get; set; }

        /// <summary>
        /// 格式化参数,数据转换时的字符串格式化参数
        /// </summary>
        public string? StringFormat { get; set; }

        /// <summary>
        /// 指定值是否在行中唯一 <see cref="DataColumn.Unique"/>
        /// </summary>
        public bool Unique { get; set; } = false;

        /// <summary>
        /// 列是否为只读 <see cref="DataColumn.ReadOnly"/>
        /// </summary>
        public bool ReadOnly { get; set; } = false;

        /// <summary>
        ///  获取或设置一个 XML 前缀，该前缀是 System.Data.DataTable 的命名空间的别名。 <see cref="DataColumn.Prefix"/>
        /// </summary>
        public string? Prefix { get; set; }

        /// <summary>
        ///  获取或设置 <see cref="DataColumn.Namespace"/> 的命名空间。 
        /// </summary>
        public string? Namespace { get; set; }

        /// <summary>
        /// 获取或设置文本列的最大长度 <see cref="DataColumn.MaxLength"/>
        /// </summary>
        public int MaxLength { get; set; } = -1;

        /// <summary>
        ///  获取或设置表达式，用于筛选行、计算列中的值或创建聚合列 <see cref="DataColumn.Expression"/>
        /// </summary>
        public string? Expression { get; set; }

        /// <summary>
        ///  获取或设置列的 DateTimeMode。 <see cref="DataColumn.DateTimeMode"/>
        /// </summary>
        public DataSetDateTime DateTimeMode { get; set; } = DataSetDateTime.UnspecifiedLocal;


        /// <summary>
        ///  获取或设置其 System.Data.DataColumn.AutoIncrement 属性设置为 true 的列使用的增量。 <see cref="DataColumn.AutoIncrementStep"/>
        /// </summary>
        public long AutoIncrementStep { get; set; } = 1;


        /// <summary>
        ///  获取或设置其 System.Data.DataColumn.AutoIncrement 属性设置为 true 的列的起始值。 <see cref="DataColumn.AutoIncrementSeed"/>
        /// </summary>
        public long AutoIncrementSeed { get; set; } = 0;

        /// <summary>
        /// 获取或设置一个值，该值指示对于添加到该表中的新行，列是否将列的值自动递增。 <see cref="DataColumn.AutoIncrement"/>
        /// </summary>
        public bool AutoIncrement { get; set; } = false;

        /// <summary>
        /// 获取或设置一个值，该值指示对于属于该表的行，此列中是否允许空值。 <see cref="DataColumn.AllowDBNull"/>
        /// </summary>
        public bool AllowDBNull { get; set; } = true;

        /// <summary>
        /// 获取或设置列的 System.Data.MappingType。 <see cref="DataColumn.ColumnMapping"/>
        /// </summary>
        public virtual MappingType ColumnMapping { get; set; } = MappingType.Element;

        /// <summary>
        /// 在创建新行时获取或设置列的默认值。 <see cref="DataColumn.DefaultValue"/>
        /// </summary>
        public object? DefaultValue { get; set; }

    }
}
